<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/23
 * Time: 13:38
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Information extends Base
{
    /**
     * 风讯列表展示
     * @return mixed|\think\response\Json
     */
    public function informationList(){
        if(request()->isAjax ()){
            $info_id = input('info_id');
            $user_create_id = input('user_create_id');
            $user_username = trim(input('user_username'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $info_is_publish = input('info_is_publish');
            $info_type = input('info_type');
            $start_time = input('start_time');
            $end_time = input('end_time');
        
            $where = [];
            if(isset($info_id) && $info_id != ''){
                $where['info_id'] = ['like', '%' . $info_id . '%'];
            }
            if(isset($user_create_id) && $user_create_id != ''){
                $where['user_create_id'] = ['like', '%' . $user_create_id . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($info_is_publish) && $info_is_publish != ''){
                $where['info_is_publish'] = $info_is_publish;
            }
            if(isset($info_type) && $info_type != ''){
                $where['info_type'] = $info_type;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['info_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['info_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['info_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('information')
                ->join('up_u_user', 'info_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('information')
                ->join('up_u_user', 'info_user_id = user_id', 'left')
                ->field('info_id, info_time, info_show, info_addr, user_username, user_create_id, 
                user_phone, info_is_publish, info_type, info_read_num, info_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('info_time desc')
                ->select();
            foreach($lists as $k => $v){
                // 时间
//                $lists[$k]['info_time'] = date('Y-m-d H:s', strtotime($v['info_time']));
                // 被举报次数
                $report_num = Db::table('up_report')
                    ->where('report_type', 1)
                    ->where('report_info_id', $v['info_id'])
                    ->count();
                $lists[$k]['report_num'] = $report_num;
                // 评论+回复数量
                $comment_ids = Db::table('up_information_comment')
                    ->where('comment_info_id', $v['info_id'])
//                    ->where('comment_status', 1)
                    ->column('comment_id');
                $comment_num = count($comment_ids);
                if($comment_num != 0){
                    $reply_num = Db::table('up_information_reply')
                        ->where('reply_comment_id', 'IN', $comment_ids)
//                        ->where('reply_status', 1)
                        ->count();
                    $lists[$k]['comment_reply_num'] = $comment_num + $reply_num;
                }else{
                    $lists[$k]['comment_reply_num'] = 0;
                }
            }
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('information/informationlist');
    }
    
    /**
     * 风讯详情
     * @return mixed
     */
    public function infoDetail(){
        $info_id = input('info_id');
        $info = Db::table('up_information')
            ->join('up_u_user', 'user_id = info_user_id', 'left')
            ->where('info_id', $info_id)
            ->field('info_content, info_images, info_type, info_addr, info_show, info_is_publish, 
            info_read_num, info_time, user_username, user_phone, user_create_id, info_id')
            ->find();
        // 图片
        if($info['info_type'] == 2){
            if(!empty($info['info_images'])){
                $info['info_images'] = explode(',', trim($info['info_images'], ','));
            }
        }
        // 被举报次数
        $report_num = Db::table('up_report')
            ->where('report_type', 1)
            ->where('report_info_id', $info_id)
            ->count();
        // 评论+回复数量
        $comment_ids = Db::table('up_information_comment')
            ->where('comment_info_id', $info_id)
//            ->where('comment_status', 1)
            ->column('comment_id');
        $comment_num = count($comment_ids);
        if($comment_num != 0){
            $reply_num = Db::table('up_information_reply')
                ->where('reply_comment_id', 'IN', $comment_ids)
//                ->where('reply_status', 1)
                ->count();
            $comment_reply_num = $comment_num + $reply_num;
        }else{
            $comment_reply_num = 0;
        }
        $this->assign([
            'info' => $info,
            'report_num' => $report_num,
            'comment_reply_num' => $comment_reply_num
        ]);
        return $this->fetch('information/infodetail');
    }
    
    /**
     * 删除风讯
     * @return \think\response\Json
     */
    public function infoDel(){
        $info_id = input('id');
        $res = Db::table('up_information')->where('info_id', $info_id)->setField('info_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 评论风讯
     * @return mixed|\think\response\Json
     */
    public function infoComment(){
        $info_id = input('info_id');
        if(Request::instance()->isPost()){
            $comment_content = trim(input('comment_content'), ' ');
            $insert = [
                'comment_user_id' => 0,
                'comment_info_id' => $info_id,
                'comment_content' => $comment_content,
                'comment_from' => 2,
                'comment_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_information_comment')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '评论成功']);
            }else{
                return json(['code' => 100, 'msg' => '评论失败']);
            }
        }
        return $this->fetch('information/infocomment', ['info_id' => $info_id]);
    }
    
    /**
     * 某条风讯的全部评论
     * @return mixed|\think\response\Json
     */
    public function oneInfoComment(){
        if(request()->isAjax ()){
            $user_create_id = input('user_create_id');
            $user_username = trim(input('user_username'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $comment_from = input('comment_from');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where['comment_info_id'] = session('info_id');
            
            if(isset($user_create_id) && $user_create_id != ''){
                $where['user_create_id'] = ['like', '%' . $user_create_id . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($comment_from) && $comment_from != ''){
                $where['comment_from'] = $comment_from;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['comment_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['comment_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['comment_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('information_comment')
                ->join('up_u_user', 'comment_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('information_comment')
                ->join('up_u_user', 'comment_user_id = user_id', 'left')
                ->field('comment_id, comment_info_id, comment_content, comment_from, user_username, user_create_id, 
                user_phone, comment_time, comment_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('comment_time desc')
                ->select();
            foreach($lists as $k => $v){
                // 被举报次数
                $report_num = Db::table('up_report')
                    ->where('report_type', 2)
                    ->where('report_comment_id', $v['comment_id'])
                    ->count();
                $lists[$k]['report_num'] = $report_num;
                // 评论+回复数量
                $reply_num = Db::table('up_information_reply')
                    ->where('reply_comment_id', $v['comment_id'])
//                    ->where('reply_status', 1)
                    ->count();
                $lists[$k]['reply_num'] = $reply_num;
                // 官方
                if($v['comment_from'] == 2){
                    $lists[$k]['user_username'] = '官方评论';
                }
            }
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $info_id = input('info_id');
        session('info_id', $info_id);
        return $this->fetch('information/oneinfocomment');
    }
    
    /**
     * 评论管理
     * @return mixed|\think\response\Json
     */
    public function commentList(){
        if(request()->isAjax ()){
            $info_id = input('info_id');
            $comment_id = input('comment_id');
            $user_create_id = input('user_create_id');
            $user_username = trim(input('user_username'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $comment_from = input('comment_from');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where = [];
        
            if(isset($info_id) && $info_id != ''){
                $where['comment_info_id'] = ['like', '%' . $info_id . '%'];
            }
            if(isset($comment_id) && $comment_id != ''){
                $where['comment_id'] = ['like', '%' . $comment_id . '%'];
            }
            if(isset($user_create_id) && $user_create_id != ''){
                $where['user_create_id'] = ['like', '%' . $user_create_id . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($comment_from) && $comment_from != ''){
                $where['comment_from'] = $comment_from;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['comment_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['comment_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['comment_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('information_comment')
                ->join('up_u_user', 'comment_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('information_comment')
                ->join('up_u_user', 'comment_user_id = user_id', 'left')
                ->field('comment_id, comment_info_id, comment_content, comment_from, user_username, user_create_id, 
                user_phone, comment_time, comment_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('comment_time desc')
                ->select();
            foreach($lists as $k => $v){
                // 被举报次数
                $report_num = Db::table('up_report')
                    ->where('report_type', 2)
                    ->where('report_comment_id', $v['comment_id'])
                    ->count();
                $lists[$k]['report_num'] = $report_num;
                // 评论+回复数量
                $reply_num = Db::table('up_information_reply')
                    ->where('reply_comment_id', $v['comment_id'])
//                    ->where('reply_status', 1)
                    ->count();
                $lists[$k]['reply_num'] = $reply_num;
                // 官方
                if($v['comment_from'] == 2){
                    $lists[$k]['user_username'] = '官方评论';
                }
            }
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('information/commentlist');
    }
    
    /**
     * 回复评论
     * @return mixed|\think\response\Json
     */
    public function commentReply(){
        $comment_id = input('comment_id');
        if(Request::instance()->isPost()){
            $reply_content = trim(input('reply_content'), ' ');
            $insert = [
                'reply_type' => 1,
                'reply_user_id' => 0,
                'reply_comment_id' => $comment_id,
                'reply_reply_id' => 0,
                'reply_content' => $reply_content,
                'reply_from' => 2,
                'reply_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_information_reply')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '评论成功']);
            }else{
                return json(['code' => 100, 'msg' => '评论失败']);
            }
        }
        return $this->fetch('information/commentreply', ['comment_id' => $comment_id]);
    }
    
    /**
     * 删除评论
     * @return \think\response\Json
     */
    public function commentDel(){
        $comment_id = input('id');
        $res = Db::table('up_information_comment')
            ->where('comment_id', $comment_id)
            ->setField('comment_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 删除并冻结用户
     * @return \think\response\Json
     */
    public function commentDelUser(){
        $comment_id = input('id');
        $comment = Db::table('up_information_comment')
            ->field('comment_user_id')
            ->where('comment_id', $comment_id)
            ->find();
        Db::startTrans();
        try{
            // 冻结账户
            Db::table('up_u_user')
                ->where('user_id', $comment['comment_user_id'])
                ->update(['user_status' => 3, 'user_update_time' => date('Y-m-d H:i:s')]);
            
            // 删除评论
            Db::table('up_information_comment')
                ->where('comment_id', $comment_id)
                ->setField('comment_status', 2);
            Db::commit();
            $res = 1;
        }catch(\Exception $e){
            Db::rollback();
            $res = 2;
        }
        if($res == 1){
            // 发短信通知用户，账号被冻结
            $user_phone = Db::table('up_u_user')->where('user_id', $comment['comment_user_id'])->value('user_phone');
            $server_phone = Db::table('up_other')->where('other_id', 4)->value('other_phone');
            YzxSms($server_phone, $user_phone, '', 4);
            return json(['code' => 200, 'msg' => '删除评论并冻结账户成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除评论并冻结账户失败']);
        }
    }
    
    /**
     * 某条评论的回复
     * @return mixed|\think\response\Json
     */
    public function oneCommentReply(){
        if(request()->isAjax ()){
            $user_create_id = input('user_create_id');
            $user_username = trim(input('user_username'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $reply_from = input('reply_from');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where['reply_comment_id'] = session('comment_id');
//            $where['reply_status'] = 1;
        
            if(isset($user_create_id) && $user_create_id != ''){
                $where['user_create_id'] = ['like', '%' . $user_create_id . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($reply_from) && $reply_from != ''){
                $where['reply_from'] = $reply_from;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['reply_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['reply_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['reply_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('information_reply')
                ->join('up_u_user', 'reply_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('information_reply')
                ->join('up_u_user', 'reply_user_id = user_id', 'left')
                ->field('reply_id, reply_content, reply_from, reply_time, user_username, user_create_id, 
                user_phone, reply_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('reply_time desc')
                ->select();
            foreach($lists as $k => $v){
                // 被举报次数
                $report_num = Db::table('up_report')
                    ->where('report_type', 3)
                    ->where('report_reply_id', $v['reply_id'])
                    ->count();
                $lists[$k]['report_num'] = $report_num;
                // 官方
                if($v['reply_from'] == 2){
                    $lists[$k]['user_username'] = '官方回复';
                }
            }
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $comment_id = input('comment_id');
        session('comment_id', $comment_id);
        return $this->fetch('information/onecommentreply');
    }
    
    /**
     * 回复列表
     * @return mixed|\think\response\Json
     */
    public function replyList(){
        if(request()->isAjax ()){
            $reply_id = trim(input('reply_id'), ' ');
            $user_create_id = input('user_create_id');
            $user_username = trim(input('user_username'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $reply_from = input('reply_from');
            $start_time = input('start_time');
            $end_time = input('end_time');
//            $where['reply_status'] = 1;
            $where = [];
        
            if(isset($reply_id) && $reply_id != ''){
                $where['reply_id'] = ['like', '%' . $reply_id . '%'];
            }
            if(isset($user_create_id) && $user_create_id != ''){
                $where['user_create_id'] = ['like', '%' . $user_create_id . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($reply_from) && $reply_from != ''){
                $where['reply_from'] = $reply_from;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['reply_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['reply_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['reply_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('information_reply')
                ->join('up_u_user', 'reply_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('information_reply')
                ->join('up_u_user', 'reply_user_id = user_id', 'left')
                ->field('reply_id, reply_content, reply_from, reply_time, user_username, user_create_id, 
                user_phone, reply_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('reply_time desc')
                ->select();
            foreach($lists as $k => $v){
                // 被举报次数
                $report_num = Db::table('up_report')
                    ->where('report_type', 3)
                    ->where('report_reply_id', $v['reply_id'])
                    ->count();
                $lists[$k]['report_num'] = $report_num;
                // 官方
                if($v['reply_from'] == 2){
                    $lists[$k]['user_username'] = '官方回复';
                }
            }
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $comment_id = input('comment_id');
        session('comment_id', $comment_id);
        return $this->fetch('information/replylist');
    }
    
    /**
     * 删除回复
     * @return \think\response\Json
     */
    public function replyDel(){
        $reply_id = input('id');
        $res = Db::table('up_information_reply')->where('reply_id', $reply_id)->setField('reply_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 回复回复
     * @return mixed|\think\response\Json
     */
    public function replyReply(){
        $reply_id = input('reply_id');
        if(Request::instance()->isPost()){
            $reply_content = trim(input('reply_content'), ' ');
            $comment_id = Db::table('up_information_reply')
                ->where('reply_id', $reply_id)
                ->value('reply_comment_id');
            $insert = [
                'reply_type' => 2,
                'reply_user_id' => 0,
                'reply_comment_id' => $comment_id,
                'reply_reply_id' => $reply_id,
                'reply_content' => $reply_content,
                'reply_from' => 2,
                'reply_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_information_reply')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '回复成功']);
            }else{
                return json(['code' => 100, 'msg' => '回复失败']);
            }
        }
        return $this->fetch('information/replyreply', ['reply_id' => $reply_id]);
    }
    
    /**
     * 删除并封ID
     * @return \think\response\Json
     */
    public function replyDelUser(){
        $reply_id = input('id');
        $reply = Db::table('up_information_reply')
            ->field('reply_user_id')
            ->where('reply_id', $reply_id)
            ->find();
        Db::startTrans();
        try{
            // 冻结账户
            Db::table('up_u_user')
                ->where('user_id', $reply['reply_user_id'])
                ->update(['user_status' => 3, 'user_update_time' => date('Y-m-d H:i:s')]);
        
            // 删除回复
            Db::table('up_information_reply')->where('reply_id', $reply_id)->setField('reply_status', 2);
            Db::commit();
            $res = 1;
        }catch(\Exception $e){
            Db::rollback();
            $res = 2;
        }
        if($res == 1){
            // 发短信通知用户，账号被冻结
            $user_phone = Db::table('up_u_user')->where('user_id', $reply['reply_user_id'])->value('user_phone');
            $server_phone = Db::table('up_other')->where('other_id', 4)->value('other_phone');
            YzxSms($server_phone, $user_phone, '', 4);
            return json(['code' => 200, 'msg' => '删除评论并冻结账户成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除评论并冻结账户失败']);
        }
    }
}